using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data;
using Crosland.Framework.DataHelper;
using Crosland.Framework.Base;
using Crosland.Offline.Core;
using Crosland.Offline.Data.Helper;

namespace Crosland.Offline.Data.Dao
{
    public class DatoComercialClienteDao : DaoBase
    {
        public DatoComercialClienteDao(string strConn) : base(strConn)
        {
        }

        DatoComercialClienteHelper helper = new DatoComercialClienteHelper();

        public int Save(DatoComercialCliente entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpSave);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, entity.ID);
            dbProvider.AddInParameter(command, helper.Vkorg, DbType.String, entity.Vkorg);
            dbProvider.AddInParameter(command, helper.Vtweg, DbType.String, entity.Vtweg);
            dbProvider.AddInParameter(command, helper.Spart, DbType.String, entity.Spart);
            dbProvider.AddInParameter(command, helper.Kdgrp, DbType.String, entity.Kdgrp);
            dbProvider.AddInParameter(command, helper.Waers, DbType.String, entity.Waers);
            dbProvider.AddInParameter(command, helper.Konda, DbType.String, entity.Konda);
            dbProvider.AddInParameter(command, helper.Pltyp, DbType.String, entity.Pltyp);
            dbProvider.AddInParameter(command, helper.Taxkd, DbType.String, entity.Taxkd);
            dbProvider.AddInParameter(command, helper.Lvgr1, DbType.String, entity.Lvgr1);
            dbProvider.AddInParameter(command, helper.Zterm, DbType.String, entity.Zterm);

            return dbProvider.ExecuteNonQuery(command);
        }

        public void Update(DatoComercialCliente entity)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpUpdate);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, entity.ID);
            dbProvider.AddInParameter(command, helper.Vkorg, DbType.String, entity.Vkorg);
            dbProvider.AddInParameter(command, helper.Vtweg, DbType.String, entity.Vtweg);
            dbProvider.AddInParameter(command, helper.Spart, DbType.String, entity.Spart);
            dbProvider.AddInParameter(command, helper.Kdgrp, DbType.String, entity.Kdgrp);
            dbProvider.AddInParameter(command, helper.Waers, DbType.String, entity.Waers);
            dbProvider.AddInParameter(command, helper.Konda, DbType.String, entity.Konda);
            dbProvider.AddInParameter(command, helper.Pltyp, DbType.String, entity.Pltyp);
            dbProvider.AddInParameter(command, helper.Taxkd, DbType.String, entity.Taxkd);
            dbProvider.AddInParameter(command, helper.Lvgr1, DbType.String, entity.Lvgr1);
            dbProvider.AddInParameter(command, helper.Zterm, DbType.String, entity.Zterm);

            dbProvider.ExecuteNonQuery(command);
        }

        public void Delete(System.String id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpDelete);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, id);

            dbProvider.ExecuteNonQuery(command);
        }

        public DatoComercialCliente GetById(System.String id)
        {
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetById);

            dbProvider.AddInParameter(command, helper.Kunnr, DbType.String, id);
            DatoComercialCliente entity = null;

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                if (dr.Read())
                {
                    entity = helper.Create(dr);
                }
            }

            return entity;
        }

        public List<DatoComercialCliente> List()
        {
            List<DatoComercialCliente> entitys = new List<DatoComercialCliente>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpList);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }

        public List<DatoComercialCliente> GetByCriteria()
        {
            List<DatoComercialCliente> entitys = new List<DatoComercialCliente>();
            DbCommand command = dbProvider.GetStoredProcCommand(helper.SpGetByCriteria);

            using (IDataReader dr = dbProvider.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    entitys.Add(helper.Create(dr));
                }
            }

            return entitys;
        }
    }
}
